Declaration & Example

byte assoc[byte];  // Associative array indexed by 'byte'
byte idx = 1;

initial begin
    // Initialize sparse entries (1, 2, 4, 8, ... , 128)
    do begin
        assoc[idx] = idx;
        idx = idx << 1;           // Shift left (multiply by 2)
    end while (idx != 0);

    // Iterate with foreach
    foreach (assoc[i])
        $display("assoc[%h] = %h", i, assoc[i]);

    // Iterate with first/next methods
    if (assoc.first(idx))         // Get first index
        do
            $display("assoc[%h] = %h", idx, assoc[idx]);
        while (assoc.next(idx));  // Get next index until none left

    // Delete specific element
    void'(assoc.first(idx));      // Get first index again
    void'(assoc.delete(idx));     // Delete that index

    // Display number of elements
    $display("The array now has %0d elements", assoc.num());
end

Key Methods:

Method Description
assoc.first(i) Returns first index in array and stores in i. Returns 0 if empty.
assoc.last(i) Returns last index.
assoc.next(i) Finds next index after i. Returns 0 if none.
assoc.prev(i) Finds previous index before i.
assoc.exists(i) Returns 1 if element i exists.
assoc.delete(i) Deletes element at index i.
assoc.num() Returns number of elements.
assoc.size() Same as .num() (alternate name).